﻿using SD.IOC.Core.Mediators;
using SlamDunk.MES.DataDrive.Common.Attributes;
using SlamDunk.MES.IAppService.DTOs.Outputs.ExecutionContext;
using SlamDunk.MES.IAppService.DTOs.Outputs.PlanContext;
using SlamDunk.MES.IAppService.DTOs.Outputs.ResourceContext;
using SlamDunk.MES.IAppService.Interfaces;
using SlamDunk.ValueObjects.Enums;
using System.Collections.Generic;
using System.Linq;

namespace SlamDunk.MES.DataDrive.Bussiness.Importers
{
    /// <summary>
    /// 导入作业
    /// </summary>
    [DataTag(16, "作业数据", "MES作业数据", DataDriveConfig.RealgoalName, DataDriveConfig.RealgoalConnectionStrings)]
    public class ImportExecution
    {
        #region # 生成工单 —— void GenerateWorkOrders()
        /// <summary>
        /// 生成工单
        /// </summary>
        [DataTag("生成工单")]
        public void GenerateWorkOrders()
        {
            IPlanContract planContract = ResolveMediator.Resolve<IPlanContract>();
            IExecutionContract executionContract = ResolveMediator.Resolve<IExecutionContract>();

            IEnumerable<ExecutionPlanInfo> executionPlanInfos = planContract.GetExecutionPlansByPage(null, null, null, null, null, null, CheckStatus.Passed, null, null, null, null, null, 1, int.MaxValue).Datas;
            foreach (ExecutionPlanInfo executionPlanInfo in executionPlanInfos)
            {
                executionContract.GenerateWorkOrders(executionPlanInfo.Id);
            }
        }
        #endregion

        #region # 分派工单 —— void DispatchWorkOrders()
        /// <summary>
        /// 分派工单
        /// </summary>
        [DataTag("分派工单")]
        public void DispatchWorkOrders()
        {
            IResourceContract resourceContract = ResolveMediator.Resolve<IResourceContract>();
            IExecutionContract executionContract = ResolveMediator.Resolve<IExecutionContract>();
            IEnumerable<WorkOrderInfo> workOrderInfos = executionContract.GetWorkOrdersByPage(null, null, null, null, null, null, null, null, null, null, null, null, null, 1, int.MaxValue).Datas;

            IList<TeamInfo> teamInfos = resourceContract.GetTeamsByPage(null, null, 1, int.MaxValue).Datas.ToList();
            IList<PersonInfo> personInfos = resourceContract.GetPersonsByPage(null, null, null, 1, int.MaxValue).Datas.ToList();
            foreach (WorkOrderInfo workOrderInfo in workOrderInfos)
            {
                int sort = workOrderInfo.Sort;
                if (sort % 2 == 0)
                {
                    //分派班组
                    TeamInfo teamInfo = teamInfos.First(x => x.FacilityId == workOrderInfo.FacilityId);
                    executionContract.DispatchWorkOrderToTeam(workOrderInfo.Id, teamInfo.Id);
                }
                else
                {
                    //分派人员
                    PersonInfo personInfo = personInfos.First(x => x.FacilityId == workOrderInfo.FacilityId);
                    executionContract.DispatchWorkOrderToPerson(workOrderInfo.Id, personInfo.Id);
                }
            }
        }
        #endregion
    }
}
